<?php

namespace app\controller;

use app\general\utils\ConversionUtils;
use app\model\MovieBasic;
use Carbon\Carbon;
use support\Db;
use support\Request;
use yzh52521\hash\Hash;


/**
 * 前台用户控制器
 */
class MovieController extends BaseController
{

   


    public function index(Request $request)
    {

        $WebUsers = Db::table('movie_basic')
            ->when(is_numeric($request->input('status')) , function ($query) use ($request) {
                return $query->where('status', $request->input('status'));
            })->when($request->input('type'), function ($query) use ($request) {
                return $query->where('type', $request->input('type'));
            })
            ->paginate($request->get('pageSize'));
        $items = $WebUsers->items();
        foreach ($items as $key=>$val){
            $items[$key] = ConversionUtils::underscoreToHump($val) ;
            $items[$key]['createTime'] = Carbon::parse($items[$key]['createTime'])->toIso8601String();
            $items[$key]['casts'] = [];
            $items[$key]['country'] = [];
            $items[$key]['moviePv'] = [];
            $items[$key]['movieRate'] = [];
        }
     
        return  json($this->success('操作成功',$items,$WebUsers->total()));
    }

    /**
     * 检查验证码
     */
    public function show(Request $request,$id)
    {
        $data=  MovieBasic::find($id);
        if(empty($data)){
            return json($this->success('操作成功',[]));
        }
        $data = $data->toArray();
        $data=ConversionUtils::underscoreToHump($data) ;

        $data['movieVideos'] = [];
        return json($this->successData('操作成功',$data));
    }

    /**
     * 创建
     * @param Request $request
     * @return \support\Response
     */
    public function store(Request $request)
    {
        $MovieBasic = new MovieBasic();
        $data = ConversionUtils::humpToUnderscore($request->all());
        unset($data['movie_videos']);
        $MovieBasic->setRawAttributes($data);
        $MovieBasic->version=0;
        $MovieBasic->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 更新
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function update(Request $request,$id)
    {
        $MovieBasic = MovieBasic::find($id);
        if(empty($MovieBasic)){
            return  json($this->success('操作失败'));
        }
        $data = $request->all();
        $data = ConversionUtils::humpToUnderscore($data);
        unset($data['create_time'],$data['updateTime'],$data['movie_videos']);
        $MovieBasic->setRawAttributes($data);
        $MovieBasic->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function destroy(Request $request, $id)
    {
        MovieBasic::destroy($id) ;
        return  json($this->success('操作成功'));
    }

}
